package com.zfy.tingshu.album.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfy.tingshu.album.service.TrackInfoService;
import com.zfy.tingshu.album.service.VodService;
import com.zfy.tingshu.common.login.TsLogin;
import com.zfy.tingshu.common.result.Result;
import com.zfy.tingshu.common.util.AuthContextHolder;
import com.zfy.tingshu.model.album.TrackInfo;
import com.zfy.tingshu.vo.album.AlbumTrackListVo;
import com.zfy.tingshu.vo.album.TrackInfoVo;
import com.zfy.tingshu.vo.album.TrackListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;
	@TsLogin(required = true)
	@Operation(summary = "获取用户声音页面")
	@PostMapping("/findUserTrackPage/{pageNum}/{size}")
	public Result findUserTrackPage(@PathVariable Integer pageNum,@PathVariable Integer size){
		Page<TrackListVo> trackInfoPage = new Page<>(pageNum, size);
		trackInfoService.findUserTrackPage(trackInfoPage);

		return Result.ok(trackInfoPage);
	}

	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId, @RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(trackId, trackInfoVo);
		return Result.ok();
	}


	@Operation(summary = "根据Id获取对象")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getById(trackId);

		return Result.ok(trackInfo);
	}


	@Operation(summary = "删除声音数据")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	@TsLogin
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
		Long userId1 = AuthContextHolder.getUserId();
		Long userId = null == userId1 ? 1l : AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(trackInfoVo, userId);
		return Result.ok();
	}
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file) {
		Map<String, Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	@Operation(summary = "根据专辑id获取声音并分页")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable("albumId")Long albumId,
									 @PathVariable("pageNo")Integer pageNo,
									 @PathVariable("pageSize")Integer pageSize){
		//获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//构建分页对象
		Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNo, pageSize);
		//调用服务层方法
		trackInfoService.findAlbumTrackPage(albumTrackListVoPage,albumId,userId);
		//返回结果
		return Result.ok(albumTrackListVoPage);
	}


}

